## This is R code for "The Demand for Insurance: Incorporating the Severity of
## Losing Office into the Insurance Model of Judicial Independence"

## R code for descriptive figures (1-3)

rm(list = ls())
## install.packages("pacman") #load all packages
## remotes::install_github("chrisadolph/simcf") since simcf cannot be directedly installed
pacman::p_load(
  tidyverse,this.path,texreg,MASS,AER,ivregEX,ivpack,simcf,arm,stargazer,ivmodel
)
## set working directory
setwd(this.path::here(..=1))
## read data
load("Data/dat.RData")


## FIGURE ONE ----------------------------------------------------------
## Percentage of former leaders punished after leaving office in three countries

india <- dat %>% filter(country_name=="India") %>% dplyr::select(c(year,punish_percent_cumsum))
costarica <- dat %>% filter(country_name=="Costa Rica") %>% dplyr::select(c(year,punish_percent_cumsum))
venezuela <- dat %>% filter(country_name=="Venezuela") %>% dplyr::select(c(year,punish_percent_cumsum))


fig1 <- ggplot(india,aes(x=year, y=punish_percent_cumsum)) + geom_line(size=1.2) +
  geom_line(data=costarica,size=1.2, linetype="dashed") +
  geom_line(data=venezuela,size=1.2, linetype="dotted") +
  scale_y_continuous(name='% of former leaders punished',
    breaks=c(0,0.2,0.4,0.6,0.8),labels=c(0,20,40,60,80),                
    limits=c(0,0.8)) +
  scale_x_continuous('Year') + 
  theme_bw(base_family='serif') + 
  theme(axis.title.x = element_text(size=16,vjust=0),
                     axis.text.x  = element_text(size=16)) + 
  theme(axis.title.y = element_text(size=16,vjust=0),
                     axis.text.y  = element_text(size=16)) +
  annotate("text", x=1978, y= 0.63, label="Venezuela",family='serif',size=6) +
  annotate("text", x=1995, y= 0.23, label="India",family='serif',size=6) +
  annotate("text", x=1965, y= 0.25, label="Costa Rica",family='serif',size=6)
ggsave(fig1,path="Figure",filename="fig1.pdf")
  
  
#FIGURE TWO ----------------------------------------------------------
## Distribution of former leaders punished after leaving office. 
fig2 <- ggplot(dat,aes(x= punish_percent_cumsum)) + stat_density(geom="line",size=1.2) +
  scale_x_continuous(name='% of former leaders punished',limits=c(0,1),
      breaks=c(0,0.25,0.50,0.75,1),labels=c(0,25,50,75,100)) +
  scale_y_continuous('Density') + 
  theme_bw(base_family='serif') + 
  theme(axis.title.x = element_text(size=16,vjust=0),
                     axis.text.x  = element_text(size=16)) + 
  theme(axis.title.y = element_text(size=16,vjust=0),
                     axis.text.y  = element_text(size=16))  
ggsave(fig2,path="Figure",filename="fig2.pdf")
                     
                     
## FIGURE THREE ----------------------------------------------------------
## The demand for insurance. 
fig3<- ggplot(dat,aes(x=threat_cumsum)) + stat_density(geom="line",size=1.2) +
  scale_x_continuous(name='Demand for insurance',limits=c(0,1.1),
      breaks=c(0,0.25,0.50,0.75,1)) +
  scale_y_continuous('Density') + 
  theme_bw(base_family='serif') + 
  theme(axis.title.x = element_text(size=16,vjust=0),
                     axis.text.x  = element_text(size=16)) + 
  theme(axis.title.y = element_text(size=16,vjust=0),
                     axis.text.y  = element_text(size=16))  
ggsave(fig3,path="Figure",filename="fig3.pdf")
                                        